<?php
/**
 * Created by PhpStorm.
 * User: liuda
 * Date: 2019/2/22
 * Time: 17:09
 */

namespace app\h5\business;


use app\common\helper\ResponseHelper;
use app\common\model\Product;
use app\common\utils\Singleton;

class ItemBusiness
{
    private static $_instance;
    public static function getInstance(){
        if(! self::$_instance instanceof self){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewItemList()
    {
        $model = new Product();
        $list = $model->getNewItemList(4);
        return self::formatH5ItemList($list);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHotItemList()
    {
        $model = new Product();
        $list = $model->getHotItemList();
        return self::formatH5ItemList($list);
    }


    private static function formatH5ItemList($list){
        if(empty($list)){
            return [];
        }
        $newList = [];
        foreach($list as $v){
            $newList[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'icon' => $v['icon'],
                'quota' => $v['quota'],
                'link' => $v['link'],
                'term' => $v['term'].'天',
                'loanTime' => $v['loanTime'].'分钟',
                'interestRate' => $v['interestRate'].'%',
                'clickCount' => $v['clickCount'],
                'created' => date('Y-m-d H:i:s', $v['created']),
                'isHot' => $v['isHot'],
                'isNew' => $v['isNew'],
                'sort' => $v['sort'],
            ];
        }
        return $newList;
    }

    public function getItemById($id)
    {
        $model = new Product();
        $info = $model->get($id);
        if(!$info){
            ResponseHelper::_failure('产品不存在');
        }
        return $info;
    }
}